import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 */
public class MyArrayList implements IMyArrayList {
    public int[] array;//成员变量是int类型的数组
    public int usedsize;//使用的数组的长度
    public static final int DEFAULT_CACPCITY = 10;

    public MyArrayList() {
        //初始化array数组,数组的长度由DEFAULT_CACPCITY的值来决定的
        this.array = new int[DEFAULT_CACPCITY];
    }
    //在列表的末尾添加data元素
    @Override
    public boolean add(int data) {
        //判断是否列表满了
        if (isFull()){
            //列表满了，进行数组扩容
            grow();
        }
        this.array[this.usedsize] = data;
        this.usedsize++;
        return false;
    }
    //判断列表是否满了
    public boolean isFull(){
        if (this.usedsize == DEFAULT_CACPCITY){
            return true;
        }
        return false;
    }
    //进行2倍扩容
    public void grow(){
        this.array = Arrays.copyOf(this.array,2*this.array.length);

    }
    public void checkPos(int pos) throws PosIllegal{
        if (pos < 0 || pos > this.usedsize){
            throw new PosIllegal("pos位置不合法");
        }
    }
    //在下标pos位置中添加data元素
    @Override
    public void add(int pos, int data) {
        try {
            //判断pos位置是否合法
            checkPos(pos);
            //判断是否满了
            if(isFull()){
                grow();
            }
            //将pos下标之后的元素往后面移一位
            for (int i = usedsize - 1; i >= pos ; i--) {
                this.array[i+1] = this.array[i];
            }
            //在pos下标插入data
            this.array[pos] = data;
            usedsize++;
        }catch (PosIllegal e){
            System.out.println("pos位置不合法！");
            e.printStackTrace();
        }

    }
//判断是否包含某个元素
    @Override
    public boolean contains(int toFind) {
        for (int i = 0; i < usedsize; i++) {
            if (array[i] == toFind){
                return true;
            }
        }
        return false;
    }
    //查找某个元素的位置
    @Override
    public int indexOf(int toFind) {
        for (int i = 0; i < usedsize; i++) {
            if (array[i] == toFind){
                return i;
            }
        }
        return -1;
    }
    public void checkEmpty(){
        if (isEmpty()){
            throw new RuntimeException("列表为空！");
        }
    }
    public boolean isEmpty(){
        if(usedsize == 0){
            return true;
        }
        return false;
    }
    //获得pos下标的元素
    @Override
    public int get(int pos) {
        try {
            checkEmpty();
            checkPos(pos);
            return array[pos];
        }catch (EmptyException e){
            e.printStackTrace();
        }catch (PosIllegal e){
            e.printStackTrace();
        }
        return -1;
    }

    @Override
    public void set(int pos, int value) {
        try {
            checkEmpty();
            checkPos(pos);
            array[pos] = value;
        }catch (EmptyException e){
            e.printStackTrace();
        }catch (PosIllegal e){
            e.printStackTrace();
        }
    }
    //获取顺序表的长度
    @Override
    public int size() {
        return this.usedsize;
    }

    @Override
    public void remove(int toRemove) {
        try{
            checkEmpty();
            //查找toRemove下标
            int pos = indexOf(toRemove);
            //删除toRemove
            for (int i = pos; i < usedsize - 1; i++) {
                array[i] = array[i + 1];
            }
            usedsize--;
        }catch (EmptyException e){
            e.printStackTrace();
        }

    }

    @Override
    public void clear() {
        usedsize = 0;
    }
    //打印顺序表
    @Override
    public void display() {
        for (int i = 0; i <this.usedsize ; i++) {
            System.out.print(array[i]+" ");
        }
    }
}
